//David Wasser
//March 10, 2009
//Software Design, Period 6
//Ultimate Frisbee

/* Team holds all of the Players and which direction
 * the team is going to score.  It also coordinates
 * whether to use an offensive or defensive strategy.
 */
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Team{

	Field field; //field associated with the Team
	Map<Integer, Player> players;
	Strategy strat;
	int activeStrat;
	int endZone;
	int side;
	static final int RIGHT = 0;
	static final int LEFT = 1;
	static final int OFF = 2;
	static final int DEF = 3;
	
	/** Creates a team on offense or defense with the
	 ** given goal side and players.
	 **/
	public Team(int pos, int endSide, Set<Player> p){
		for(Player player : p)
			player.setTeam(this);
		
		side = pos;
		endZone = endSide;
		players = new HashMap<Integer, Player>();
		Iterator<Player> itr = p.iterator();
		for(int i = 0; itr.hasNext(); i++)
			players.put(i, itr.next());
		if(side == Field.HOME){
			activeStrat = OFF;
			strat = new OffStack(this, null);
		}else{
			activeStrat = DEF;
			strat = new DefStack(this, null);
		}//End else
	}//End Team()
	
	/** Returns the opposing team.
	 **/
	public Team getOtherTeam(){
		if(field.getHomeTeam() == this)
			return field.getAwayTeam();
		else
			return field.getHomeTeam();
	}//End getOther()
	
	/** Is called by the other team to get the current
	 ** strategy.
	 **/
	public Strategy getDefStrategy(){
		return strat;
	}//End getDefStrategy()
	
	/** Returns true if a Player is on the team.
	 **/
	public boolean isPlayerOnTeam(Player player){
		return players.containsValue(player);
	}//end isPlayerOnTeam()
	
	/** Returns the Set of all the Players on the team.
	 **/
	public Set<Player> getPlayers(){
		HashSet<Player> playerlist = new HashSet<Player>();
		for(Integer i : players.keySet())
			playerlist.add(players.get(i));
		return playerlist;
	}//end getPlayers()
	
	/** This is called whenever the disc is either caught
	 ** or thrown with the catcher/thrower as the input.
	 **/
	public void discChange(Player player){
		if(field.disc.isHeld() && isPlayerOnTeam(player)){
			if(activeStrat == DEF){
				strat = new OffStack(getPlayers(), this, field);
				activeStrat = OFF;
			}//End if
		}else if(activeStrat == OFF){
			strat = new DefStack(getPlayers(), this, field);
			activeStrat = DEF;
		}//End if
		strat.discChange(player);
	}//End discChange()
	
	/** Sets the field of the Team and current Strategy
	 ** to the input Field.
	 **/
	public void setField(Field gameField){
		field = gameField;
		strat.setField(gameField);
	}//End setField()
}//End Team